package com.gilight.base.controller;

import com.gilight.base.aspect.annotation.OperatorLog;
import com.gilight.base.base.ResultEntity;
import com.gilight.base.model.SysZzjg;
import com.gilight.base.service.SysUserZzjgService;
import com.gilight.base.service.SysZzjgService;
import com.gilight.base.util.MapUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/web/trDept")
public class SysZzjgController {

    @Resource
    private SysZzjgService sysZzjgService;
    @Resource
    private SysUserZzjgService sysUserZzjgService;

    @ApiOperation(value = "根据条件查询组织机构")
    @RequestMapping(value = "/admin/getDepartmentList", method = RequestMethod.GET,name="根据条件查询组织机构")
    @OperatorLog(value = "根据条件查询组织机构",operateType= "查询")
    public ResultEntity getDepartmentList(@RequestParam String searchName) {
        ResultEntity resultEntity = new ResultEntity();
        Map<String, Object> map = new HashMap<>();
        map.put("searchName", searchName);
        map.put("parentId", "-1");
        List<Map<String,Object>> zzjgsUser = sysUserZzjgService.getCurrentUserZzjgs();
        map.remove("parentId", "-1");
        if(zzjgsUser!=null&&zzjgsUser.size()>0){

            map.put("idSpe",zzjgsUser.get(0).get("zzjgId"));
        }else{
            map.put("idSpe",1);
        }
        List<Map<String, Object>> deptList = sysZzjgService.findMapListCte(map);
        Map<Object, List<Map<String, Object>>> parent_id = deptList.stream().collect(Collectors.groupingBy((item) -> {
            HashMap itemHash = (HashMap) item;
            return itemHash.get("parent_id");
        }));

        List<Map<String, Object>> deptListN = new ArrayList<>();
        parent_id.forEach((k,v)->{
                v.forEach(item->{
                    Long id = Long.valueOf(String.valueOf(item.get("id")));
                    item.put("children",parent_id.get(id));
                    this.cetZZJG(parent_id,parent_id.get(id));
                });
        });
        deptListN = deptList.stream().filter(item->{
            String idSpe = (String.valueOf( map.get("idSpe")));
            String currentId = (String.valueOf(item.get("id")));
            return idSpe.equals(currentId);
        }).collect(Collectors.toList());
        resultEntity.setData(deptListN);
        return resultEntity;
    }

    public void cetZZJG(Map<Object, List<Map<String, Object>>> parent_id,List<Map<String, Object>> child){
        if(child!=null) {
            child.forEach(item -> {
                Long id = Long.valueOf(String.valueOf(item.get("id")));
                item.put("children", parent_id.get(id));
            });
        }
    }


    @ApiOperation(value = "查询组织机构类型")
    @RequestMapping(value = "/admin/getZzjgType", method = RequestMethod.GET,name="查询组织机构类型")
    @OperatorLog(value = "查询组织机构类型",operateType= "查询")
    public ResultEntity getZzjgType() {
        ResultEntity resultEntity = new ResultEntity();
        List<Map<String, Object>> deptList = sysZzjgService.getZzjgType();
        resultEntity.setData(deptList);
        return resultEntity;
    }
    @ApiOperation(value = "查询组织机构所属品牌列表")
    @RequestMapping(value = "/admin/getZzjgBrand", method = RequestMethod.GET,name="查询组织机构类型")
    @OperatorLog(value = "查询组织机构类型",operateType= "查询")
    public ResultEntity getZzjgBrand() {
        ResultEntity resultEntity = new ResultEntity();
        List<Map<String, Object>> deptList = sysZzjgService.getZzjgBrand();
        resultEntity.setData(deptList);
        return resultEntity;
    }
    @ApiOperation(value = "根据条件查询组织机构")
    @RequestMapping(value = "/admin/getDepartmentList2", method = RequestMethod.GET,name="根据条件查询组织机构")
    @OperatorLog(value = "根据条件查询组织机构",operateType= "查询")
    public ResultEntity getDepartmentList2(@RequestParam String parentId) {
        ResultEntity resultEntity = new ResultEntity();
        Map<String, Object> map = new HashMap<>();
        map.put("parentId", parentId);
        List<Map<String, Object>> deptList = sysZzjgService.findMapList(map);
        resultEntity.setData(deptList);
        return resultEntity;
    }
    @ApiOperation(value = "新增组织机构")
    @RequestMapping(value = "/admin/addDepartment", method = RequestMethod.POST,name="新增组织机构")
    @OperatorLog(value = "新增组织机构",operateType= "添加")
    public ResultEntity addDepartment(@RequestBody SysZzjg sysZzjg) {
        ResultEntity resultEntity = new ResultEntity();
        sysZzjgService.addSysZzjg(sysZzjg);
        return resultEntity;
    }
    @ApiOperation(value = "编辑组织机构")
    @RequestMapping(value = "/admin/editDepartment", method = RequestMethod.POST,name="编辑组织机构")
    @OperatorLog(value = "编辑组织机构",operateType= "修改")
    public ResultEntity editDepartment(@RequestBody SysZzjg sysZzjg) {
        ResultEntity resultEntity = new ResultEntity();
        sysZzjgService.editSysZzjg(sysZzjg);
        return resultEntity;
    }
    @ApiOperation(value = "删除组织机构")
    @RequestMapping(value = "/admin/delDepartment", method = RequestMethod.GET,name="删除组织机构")
    @OperatorLog(value = "删除组织机构",operateType= "删除")
    public ResultEntity delDepartment(@RequestParam Long id) {
        ResultEntity resultEntity = new ResultEntity();
        sysZzjgService.delDepartment(id);
        return resultEntity;
    }
    @ApiOperation(value = "根据id查询组织机构")
    @RequestMapping(value = "/admin/getSysZzjgById", method = RequestMethod.GET,name="根据id查询组织机构")
    @OperatorLog(value = "根据id查询组织机构",operateType= "查询")
    public ResultEntity getSysZzjgById(@RequestParam Long id) {
        ResultEntity resultEntity = new ResultEntity();
        SysZzjg s=sysZzjgService.getSysZzjgById(id);
        resultEntity.setData(s);
        return resultEntity;
    }

    @ApiOperation(value = "获取所有二级部门")
    @OperatorLog(value = "获取所有二级部门", operateType = "查询")
    @RequestMapping(value = "/web/getZzjgList", method = {RequestMethod.GET})
    public ResultEntity getZzjgList() {
        ResultEntity resultEntity = new ResultEntity();
        List<Map<String, Object>> map = sysZzjgService.getZzjgList();
        resultEntity.setData(map);
        return resultEntity;
    }

    @ApiOperation(value = "根据部门id、部门code、部门名称查询组织机构")
    @OperatorLog(value = "根据部门id、部门code、部门名称查询组织机构", operateType = "查询")
    @RequestMapping(value = "/web/getZzjgData", method = { RequestMethod.POST })
    public ResultEntity getZzjgData(@RequestBody Map<String,Object>map) {
        ResultEntity resultEntity = new ResultEntity();
        List<Map<String,Object>> list = sysZzjgService.getZzjgData(map);
        resultEntity.setData(list);
        return resultEntity;
    }
}
